//
//  WSHomeViewController.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/4.
//  Copyright © 2023 SiterWell.Co.,Ltd. All rights reserved.
//

#import "WSDeviceHomeViewController.h"

static const CGFloat kAddbuttonHeight = 64;

@interface WSDeviceHomeViewController ()

@end

@implementation WSDeviceHomeViewController

+ (WSDeviceHomeViewController *)showController:(UIViewController *) viewController {
    WSDeviceHomeViewController *vc = [[WSDeviceHomeViewController alloc] init];
    
    HBDNavigationController *rootNav = [[HBDNavigationController alloc] initWithRootViewController:vc];
    [UIApplication sharedApplication].keyWindow.rootViewController = rootNav;
    return vc;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.hbd_barHidden = YES;
    [self setupSubviews];
    
    
}


// MARK: 添加子控件
- (void)setupSubviews {
    // 启用屏幕边缘拖拽出左侧菜单
    [WSLeftMenuView enableScreenEdgeDraggingInView:self.view];
    if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self setupLeftItemButton];
//    [self setupRightItemButton];
    [self setupPageViewControllers];
    [self setupTableView];
    [self setupAddDeviceButton];
    
}

- (void)setupLeftItemButton {
    UIButton *menuDeviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuDeviceBtn setFrame:CGRectMake(0, 0, 40, 40)];
    [menuDeviceBtn setImage:ImageNamed(@"iv_draw_left") forState:UIControlStateNormal];
    [menuDeviceBtn addTarget:self action:@selector(menuDeviceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    if (_ios11_0_){
        [menuDeviceBtn setFrame:CGRectMake(0, 0, 40, 40)];
        // rightBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 10);
    }
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:menuDeviceBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

- (void)setupRightItemButton {
    UIButton *addDeviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addDeviceBtn setFrame:CGRectMake(0, 0, 40, 40)];
    [addDeviceBtn setImage:ImageNamed(@"main_add") forState:UIControlStateNormal];
    [addDeviceBtn addTarget:self action:@selector(addDeviceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    if (_ios11_0_){
        [addDeviceBtn setFrame:CGRectMake(0, 0, 40, 40)];
        // rightBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 10);
    }
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:addDeviceBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[WSDeviceHomeCell class] forCellReuseIdentifier:NSStringFromClass([WSDeviceHomeCell class])];
}

- (void)setupAddDeviceButton {
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:addButton];
    addButton.frame = CGRectMake(kScreenWidth - 32 - kAddbuttonHeight, kScreenHeight - 64 - kAddbuttonHeight - [WSDeviceHelper safeAreaInsetsBottom] , kAddbuttonHeight, kAddbuttonHeight);
    [self.view bringSubviewToFront:addButton];
    [addButton setBackgroundImage:ImageNamed(@"iv_main_add_nor") forState:UIControlStateNormal];
    [addButton setBackgroundImage:ImageNamed(@"iv_main_add_sel") forState:UIControlStateSelected];
    [addButton addTarget:self action:@selector(addDeviceButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
}

/**设置segmentedPageViewController的pageViewControllers和categoryView
 * 这里用到的pageViewController需要继承自HGPageViewController
 */
- (void)setupPageViewControllers {
    //    [self.tableView registerClass:[WSDeviceHomeCell class] forCellReuseIdentifier:NSStringFromClass([WSDeviceHomeCell class])];

    NSArray *titles = @[@"所有设备", @"客厅", @"餐厅", @"厨房"];
    for (int i = 0; i < titles.count; i++) {
        WSScenesViewController *controller = [[WSScenesViewController alloc] initWithTitle:titles[i]];
        [self.scenesControllers addObject:controller];
    }
    self.segmentedPageViewController.pageViewControllers = self.scenesControllers;
    self.segmentedPageViewController.selectedPage = 2;
    self.segmentedPageViewController.categoryView.titles = titles;
    self.segmentedPageViewController.categoryView.alignment = HGCategoryViewAlignmentLeft;
}

// MARK: lazy


- (NSMutableArray *)scenesControllers {
    if (!_scenesControllers) {
        _scenesControllers = [NSMutableArray new];
    }
    return _scenesControllers;
}



// MARK: action

- (void)addDeviceBtnClick {
    
}

- (void)menuDeviceBtnClick {
    
    [WSLeftMenuView showView];
}

- (void)addDeviceButtonAction {
    [WSAddDeviceBluetoothViewController showController:self];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSDeviceHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSDeviceHomeCell class]) forIndexPath:indexPath];
    return cell;
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}


@end
